﻿//========================================================= 
/**@file LogGroupAppender.h
 * @brief 可同时向多个Appender输出日志
 * 
 * @date 2018-07-06   13:46:57
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_LOGGROUPAPPENDER_H_
#define _LIBZHOUYB_LOGGROUPAPPENDER_H_
//--------------------------------------------------------- 
#include "../ILogCat.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace extension {
namespace logcat {
//--------------------------------------------------------- 
/// 将多个Appender绑定为单个Appender,用于接口适配
class LogGroupAppender : public ILogAppender, public RefObject
{
protected:
    ASYNC_USING(async_mutex _mutex);
    bool _is_enable;
    list_t<Ref<ILogAppender> > _appenders;
public:
    LogGroupAppender(bool is_enable = true) : _is_enable(is_enable) {}
    bool contains(const Ref<ILogAppender>& appender) const
    {
        return list_helper<Ref<ILogAppender> >::contains(_appenders, appender);
    }
    bool add(const Ref<ILogAppender>& appender)
    {
        ASYNC_LOCK(_mutex);
        if(!contains(appender))
        {
            _appenders.push_back(appender);
            return true;
        }
        return false;
    }
    bool remove(const Ref<ILogAppender>& appender)
    {
        ASYNC_LOCK(_mutex);
        if(!contains(appender))
            return false;
        list_helper<Ref<ILogAppender> >::remove(_appenders, appender);
        return true;
    }
    inline const bool& enable() const
    {
        return _is_enable;
    }
    inline bool& enable()
    {
        return _is_enable;
    }
    inline void clear()
    {
        ASYNC_LOCK(_mutex);
        _appenders.clear();
    }
    virtual void append(LogLevel lv, const char* tag, const char* str)
    {
        if(!_is_enable)
            return;
        ASYNC_LOCK(_mutex);
        list_t<Ref<ILogAppender> >::iterator itr;
        for(itr = _appenders.begin(); itr != _appenders.end(); ++itr)
        {
            if(!(itr->IsNull()))
            {
                (*itr)->append(lv, tag, str);
            }
        }
    }
};
//--------------------------------------------------------- 
} // namespace logcat
} // namespace extension
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_LOGGROUPAPPENDER_H_
//========================================================= 